#include "StdAfx.h"
#include "SpellListXmlConfigAdapter.h"
#include <map>
#include <comutil.h>

CSpellListXmlConfigAdapter::CSpellListXmlConfigAdapter(LPCTSTR strTempName)
{
	strFileName = strTempName;
}

CSpellListXmlConfigAdapter::~CSpellListXmlConfigAdapter(void)
{
}

bool CSpellListXmlConfigAdapter::WriteDataToFile()
{
	return false;
}

bool CSpellListXmlConfigAdapter::ReadFile()
{
	HRESULT hr;
	MSXML2::IXMLDOMDocumentPtr pDoc;
	//const CLSID CLSID_DOMDocument;
	hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument));
    pDoc->load( _variant_t(strFileName));

	//HRSRC hrz;
	//HGLOBAL hz;
	//hrz = ::FindResource(::GetModuleHandle(_T("Melete.dll")),_T("IDR_SPELLLIST1"),_T("SpellList"));
	//CString str;
	//str.Format(_T("%x"),hrz);
	//::MessageBox(NULL,str,_T(""),MB_OK);
	//if(hrz)
	//{
	//	ULONG nResSize = ::SizeofResource(NULL,hrz) ;
	//	if(hz = ::LoadResource(NULL,hrz))
	//	{
	//		char* pXMLBuffer = (char*)hz;
	//		pDoc->load( _variant_t(pXMLBuffer));
	//		if(pDoc != NULL)
	//			::MessageBox(NULL,_T("1"),_T(""),MB_OK);
	//		else
	//			::MessageBox(NULL,_T("0"),_T(""),MB_OK);
	//	} 
	//}


	MSXML2::IXMLDOMElementPtr  pDocElement;
	MSXML2::IXMLDOMElementPtr  pParElement;

	

	pDocElement=pDoc->GetdocumentElement();
    if (pDocElement == NULL)
	{
		pDoc->save(_variant_t(strFileName));
	    return false;
	}

	if (pDocElement->hasChildNodes())
	{

		MSXML2::IXMLDOMElementPtr pPos;
		MSXML2::IXMLDOMElementPtr pLastPos;

		pPos = pDocElement->GetfirstChild();
		do
		{
			CString strSpellName;
			CString strSpellNameByLang;

			strSpellName = (LPCTSTR)(_bstr_t)pPos->getAttribute(_T("name"));
			strSpellNameByLang = (LPCTSTR)(_bstr_t)pPos->getAttribute(_bstr_t(m_strType));
			
			m_SpellListByLang[strSpellName] = strSpellNameByLang;

			if (pPos == NULL)
			{
				pDoc->save(_variant_t(strFileName));
				return false;
			}

			pPos = pPos->GetnextSibling();
			if(pPos == NULL)break;

		}while(1);
	}

	pDoc->save(_variant_t(strFileName));
	return true;
}

bool CSpellListXmlConfigAdapter::WriteToLacol(std::map<CString,CString> &TempList)
{
	m_SpellListByLang = TempList;
	return (WriteDataToFile());
}

bool CSpellListXmlConfigAdapter::ReadToList(std::map<CString,CString> &TempList,CString strType)
{
//	CFileFind Finder;
//    if (!Finder.FindFile(strFileName))
	m_strType = strType;
	if (!IsFileExist(strFileName))
    {
		m_SpellListByLang.clear();
	    return true;
    }

	if (!ReadFile())
		return false;

	TempList = m_SpellListByLang;
	return true;
}